package com.firefly.admin.common.utils;

import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.firefly.admin.common.constant.Constants;

import javax.servlet.http.HttpServletRequest;

public class DeviceUtil {
    /**
     * 获取设备系统名称
     *
     * @return
     */
    public static String getSystemName(HttpServletRequest request) {
        String userAgentStr = request.getHeader(Constants.USER_AGENT);
        UserAgent userAgent = UserAgentUtil.parse(userAgentStr);
        if (userAgent != null) {
            return userAgent.getOs().getName();
        }
        return null;
    }

    /**
     * 获取浏览器信息
     *
     * @param request 请求
     * @return 浏览器信息和版本号
     */
    public static String getBrowserInfo(HttpServletRequest request) {
        String browserName = "";
        String versionStr = "";
        String userAgentStr = request.getHeader(Constants.USER_AGENT);
        UserAgent userAgent = UserAgentUtil.parse(userAgentStr);
        if (userAgent != null) {
            browserName = userAgent.getBrowser().getName();
            versionStr = userAgent.getBrowser().getVersion(userAgentStr);
        }
        return browserName + " " + versionStr;
    }
}
